package com.silence.gankio.fragment

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.silence.gankio.R
import com.silence.gankio.activity.web.AgentWebActivity
import com.silence.gankio.activity.web.AgentWebFragment.KEY_URL
import com.silence.gankio.adapter.GankAndroidAdapter
import com.silence.gankio.bean.GankIoData
import com.silence.gankio.mvvm.viewModel.GankAndroidViewModel
import kotlinx.android.synthetic.main.gank_android_fragment.*

class GankAndroidFragment : GankIoFragment<GankAndroidViewModel>(), SwipeRefreshLayout.OnRefreshListener {

    override fun getFragmentContentView() = R.layout.gank_android_fragment

    private val mAdapter by lazy { GankAndroidAdapter(null) }
    private var page = 1
    private var first = true

    companion object {
        fun newInstance() = GankAndroidFragment()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initListener()

        refresh_layout.post {
            refresh_layout.isRefreshing = true
            loadData(true)
        }
    }

    override fun getModelClass() = GankAndroidViewModel::class.java


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recycler.layoutManager = LinearLayoutManager(mContext)
        mAdapter.openLoadAnimation()
        // 当列表滑动到倒数第N个Item的时候(默认是1)回调onLoadMoreRequested方法
        mAdapter.setPreLoadNumber(4)

        refresh_layout.setOnRefreshListener(this)

        recycler.adapter = mAdapter
    }

    private fun loadData(showLoadingUI: Boolean) {
        mBaseModel?.loadData(showLoadingUI, page, 10)
    }

    override fun onRefresh() {
        if (mAdapter.isLoading) {
            return
        }
        page = 1
        first = true
        loadData(false)
    }

    private fun initListener() {
        mBaseModel?.getAndroidData()?.observe(this, object : Observer<List<GankIoData>> {
            override fun onChanged(t: List<GankIoData>?) {
                mAdapter.loadMoreComplete()
                if (t.isNullOrEmpty()) {
                    mAdapter.loadMoreFail()
                } else {
                    page++
                    if (first) {
                        first = false
                        mAdapter.setNewData(t)
                        mAdapter.disableLoadMoreIfNotFullPage(recycler)
                    } else {
                        mAdapter.addData(t)
                    }
                }
                refresh_layout.isRefreshing = false
                mAdapter.setEnableLoadMore(true)
            }
        })

        mAdapter.setOnLoadMoreListener({
            loadData(false)
        }, recycler)

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val gankData: GankIoData = adapter.data[position] as GankIoData
            val intent = Intent(mContext, AgentWebActivity::class.java)
            intent.putExtra(KEY_URL, gankData.url)
            startActivity(intent)

        }
    }

}
